<template>
  <a-layout class="main-layout">
    <!-- 侧边栏卡片 -->
    <a-card
        class="sider-card"
        :bordered="false"
        :style="{
        width: collapsed ? '90px' : '300px',
        left: collapsed ? '24px' : '24px'
      }"
    >
      <a-layout-sider
          v-model:collapsed="collapsed"
          :trigger="null"
          collapsible
          theme="light"
          :width="256"
          :collapsedWidth="60"
      >
        <div class="logo">
          <img src="../../public/img/icon.png" class="logo-image" :class="{ 'collapsed-logo': collapsed }" alt="">
          <span v-if="!collapsed" class="logo-text">Chenxin</span>
        </div>
        <a-menu
            v-model:selectedKeys="selectedKeys"
            theme="light"
            mode="inline"
            :inlineCollapsed="collapsed"
            class="custom-menu"

        >
          <a-menu-item key="index" class="centered-menu-item">
            <template #icon><BookTwoTone /></template>
            <span>首页</span>
          </a-menu-item>
          <a-menu-item key="user" class="centered-menu-item">
            <template #icon><user-outlined /></template>
            <span>用户</span>
          </a-menu-item>
          <a-menu-item key="video" class="centered-menu-item">
            <template #icon><video-camera-outlined /></template>
            <span>视频</span>
          </a-menu-item>
          <a-menu-item key="logout" class="centered-menu-item">
            <template #icon><LockTwoTone /></template>
            <span>注销</span>
          </a-menu-item>
        </a-menu>
      </a-layout-sider>
    </a-card>

    <!-- 主内容区域 -->
    <a-layout
        :style="{
        marginLeft: collapsed ? '60px' : '280px',
        transition: 'margin 0.2s',
      }"
    >
      <!-- 去除边框的头部 -->
      <a-layout-header class="header" :style="{ left: collapsed ? '120px' : '340px' }">
        <div class="header-content">
          <div class="header-left">
            <menu-unfold-outlined
                v-if="collapsed"
                class="trigger"
                @click="toggleCollapsed"

            />
            <menu-fold-outlined v-else class="trigger" @click="toggleCollapsed" />
          </div>
          <div class="header-right">
            <a-dropdown-button>
              User
              <template #overlay>
                <a-menu @click="handleMenuClick">
                  <a-menu-item key="1">
                    <UserOutlined />
                    个人中心
                  </a-menu-item>
                  <a-menu-item key="2" @click="Logout">
                    <ExclamationCircleTwoTone />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
              <template #icon><UserOutlined /></template>
            </a-dropdown-button>

          </div>
        </div>
      </a-layout-header>

      <a-layout-content class="content">
        <div class="content-wrapper">
          <router-view />
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useRouter,useRoute } from 'vue-router';
import {
  UserOutlined,
  VideoCameraOutlined,
  MenuUnfoldOutlined,
  MenuFoldOutlined,LockTwoTone,
  ExclamationCircleTwoTone,
    BookTwoTone
} from '@ant-design/icons-vue';
import {logout,Test} from '../api/login.js'
const response = ref(null);
import {onMounted} from "vue";
const selectedKeys = ref([]);
const collapsed = ref(false);
const router = useRouter();
const route = useRoute();
import useAuthStore from "@/stores/auth.js";
const authStore = useAuthStore();
const toggleCollapsed = () => {
  collapsed.value = !collapsed.value;
};
// 路由名称到菜单key的映射
const routeToMenuKey = {
  'Index': 'index',
  'MainUser': 'user',
  'MainVideo': 'video',
  'Logout': 'logout'
};
watch(selectedKeys, (value) => {
  const routeMap = {
    index: 'Index',
    user: 'MainUser',
    video: 'MainVideo',
    logout: 'Logout',
  };
  router.push({ name: routeMap[value[0]] });
});
onMounted(() => {
  const currentRoute=route.name;
  console.log(currentRoute);
  const menuKey = routeToMenuKey[currentRoute];
  if (menuKey) {
    selectedKeys.value[0] = menuKey;
  }
})
async function Logout() {
  try{
    console.log('开始登出');
    const res = await logout();
    response.value = res.data;
    if (response.value.code === 200){
      router.push('/login');
      authStore.logoutSuccess();
    }
  }catch(err){
    console.log(err)
  }
}
</script>

<style scoped>
.main-layout {
  min-height: 100vh;
  background: #f0f2f5;
}

/* 侧边栏卡片样式 */
.sider-card {
  width: 500px;
  height: auto;
  position: fixed;
  top: 10px;
  bottom: 10px;
  z-index: 100;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  overflow: visible !important;
}

/* 头部样式 */
.header {
  position: fixed;
  top: 0px;
  right: 24px;
  z-index: 101;
  height: 64px;
  left: auto;
  padding: 0 !important;
  background: #F5F5F5 !important;
  border: none !important; /* 移除边框 */
  box-shadow: none !important; /* 移除阴影 */
  transition: all 0.2s;
}



.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 24px;
  border-bottom: 1px solid #f0f0f0; /* 可选分割线 */
}

/* 内容区域 */
.content {
  margin-top: 60px;
  margin-left: 60px;
  margin-right: 24px;
  padding: 24px;
  background: #F5F5F5 !important;
  min-height: calc(100vh - 112px);
  border-radius: 8px;
  overflow: auto;
}

/* 侧边栏元素 */
.logo {
  height: 64px;
  margin: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  background: white;
  border-radius: 4px;
  transition: all 0.2s;
}

.custom-menu {
  border-right: none !important;
  width: auto !important
}

.trigger {
  font-size: 18px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .sider-card {
    left: 12px !important;
    top: 12px;
    bottom: 12px;
  }

  .header {
    top: 12px;
    right: 12px;
    left: calc(80px + 12px);
    height: 56px;
  }

  .content {
    margin-top: 76px;
    margin-left: 12px;
    margin-right: 12px;
    padding: 16px;
  }

  .logo {
    height: 48px;
    margin: 12px;
    font-size: 16px;
  }
}
/* 删除之前的居中样式，替换为： */
:deep(.centered-menu-item) {
  /* 保持菜单项宽度自适应 */
  width: auto !important;
  padding: 0 16px !important;
}

:deep(.centered-menu-item .ant-menu-title-content) {
  font-size: 16px !important;
  font-weight: 500; /* 可选：加粗文字 */
}

:deep(.centered-menu-item .anticon) {
  font-size: 18px !important;
  margin-right: 12px !important;
  vertical-align: middle !important;
}
.logo {
  height: 64px;
  margin: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 新增以下样式 */
  gap: 12px;
  padding: 0 8px;
  overflow: hidden;
  transition: all 0.2s;
}

.logo-image {
  width: 40px;
  height: 40px;
  transition: all 0.2s;
}

.logo-image.collapsed-logo {
  width: 30px;
  height: 30px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  white-space: nowrap;
  color: #1890ff;
  transition: opacity 0.2s;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .logo-image {
    width: 34px;
    height: 34px;
  }

  .logo-image.collapsed-logo {
    width: 28px;
    height: 28px;
  }

  .logo-text {
    font-size: 16px;
  }
}

</style>
